﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;

[AddComponentMenu("PUEX/Affector/UV/UVOffsetOverLifetime")]
public class PUEXUVOffsetOverLifetimeAffector : PUEXAffector {

	#region - Editable property
	public PUEXDynamicAttribute offsetUOverLifetime;
	public PUEXDynamicAttribute offsetVOverLifetime;
	public bool useEmitterTime;
	#endregion

	#region - Lifecycle
	public PUEXUVOffsetOverLifetimeAffector ()
	{
		offsetUOverLifetime = PUEXDynamicAttribute.GenerateFixed (0f);
		offsetVOverLifetime = PUEXDynamicAttribute.GenerateFixed (0f);
		useEmitterTime = false;
	}
	#endregion

	#region - Update
	public override void update (float dt, PUEXParticle particle)
	{
		float t = particle.timeFraction;
		if (useEmitterTime)
			t = inTrigger () ? triggerTimeFraction : ownerEmitter.timeFraction;

		particle.uvOffset = particle.originalUVOffset + new Vector2 (offsetUOverLifetime.getValue(t), offsetVOverLifetime.getValue(t));
	}
	#endregion

	#region - Param property
	public override void updateParams (PUEXParticleSystem system)
	{
		offsetUOverLifetime.parameterize (system);
		offsetVOverLifetime.parameterize (system);
	}
	#endregion

	#region - Serialization
	public override JsonData save (JsonData emitterData, JsonData systemData)
	{
		JsonData ret = base.save (emitterData, systemData);

		ret ["offset_u_over_lifetime"] = PUEXUtils.serializeDynamicAttribute(offsetUOverLifetime);
		ret ["offset_v_over_lifetime"] = PUEXUtils.serializeDynamicAttribute(offsetVOverLifetime);
		ret ["use_emitter_time"] = useEmitterTime;
		
		return ret;
	}
	#endregion

}
